<?php

/* 
 * API插件控制器
 * @author wyb
 */
namespace Qwadmin\Controller;
class ApiPlugController extends ComController {
    
    private $_plug_data = null;
    
    private $_model = null;
    
    public function _initialize() {
        parent::_initialize();
        
        //获得所有的插件
        $path_data = APP_PATH . '/Api/Api/';
        if( !file_exists( $path_data ) ) {
            exit( 'API目录错误' );
        }
        $file_path = opendir( $path_data );
        while( $api_dir = readdir( $file_path ) ) {
            if( in_array( $api_dir , array('.','..') ) ) continue;
            $plug_conf_path = $path_data . '/' . $api_dir . '/_Init.php';
            if( file_exists( $plug_conf_path ) ) {
                $config_data = include( $plug_conf_path );
                $config_data['status'] = 0; //未安装
                if( $module_data = M('api_module')->where(array('module' => $config_data['module']))->find() ) {
                    if( $module_data['version'] == $config_data['version'] ) {
                        $config_data['status'] = 2; //已安装
                    } else {
                        $config_data['status'] = 1; //可以升级
                        $config_data['version'] = $config_data['version'];
                        $config_data['org_version'] = $module_data['version'];
                    }
                }
                $this->_plug_data[$config_data['module']] = $config_data;
            }
        }

        $this->_model = M('api_module');
    }
    
    /**
     * 插件列表
     */
    public function Index() {
        $this->assign( 'lists' , $this->_plug_data );
        $this->display();
    }
    
    /**
     * 插件安装
     */
    public function Install() {
        $module = I('get.data') ? I('get.data') : $this->error( '信息错误' );
        $this->_Install( $module );
        $this->success( '插件安装成功' );
    }
    
    /**
     * 卸载插件
     */
    public function UnInstall() {
        $module = I('get.data') ? I('get.data') : $this->error( '信息错误' );
        $this->_UnInstall( $module );
        $this->success( '卸载成功' );
    }
    
    /**
     * 插件升级
     */
    public function Upgrade(){
        $module = I('get.data') ? I('get.data') : $this->error( '信息错误' );
        $this->_UnInstall( $module );
        $this->_Install( $module );
        $this->success( '升级成功' );
    }
    
    /**
     * 卸载
     */
    private function _UnInstall( $module ) {
        $this->_model->startTrans();
        if( $module_data = $this->_model->where(array('module' => $module))->find() ) {
            if( $this->_model->where(array('module' => $module))->delete() === false ) {
                $this->error( '卸载出错，请重新再试！' );
            }
            if( M('api_method')->where(array('module_id' => $module_data['id']))->delete() === false ) {
                $this->_model->rollback();
                $this->error( '卸载出错，请重新再试！' );
            }
            $this->_model->commit();
        }
    }
    
    /**
     * 安装
     */
    private function _Install( $module ) {
        if( !isset( $this->_plug_data[ $module ] ) || !$this->_plug_data[ $module ] ) {
            $this->error( '安装插件数据有问题，请核对！' );
        }
        $plug_data = $this->_plug_data[ $module ];
        $plug_id = $this->_model->add(array(
            'module'        => $plug_data['module'],
            'description'   => $plug_data['description'],
            'title'         => $plug_data['title'],
            'version'       => $plug_data['version'],
            'create_time'   => time()
        ));
        foreach( $plug_data['method'] as $key => $value ) {
            M('api_method')->add(array(
                'title'  => $value['title'],
                'method' => $value['method'],
                'description'   => $value['description'],
                'allow_method'  => $value['allowMethod'],
                'mode_type'     => $value['mode_type'],
                'module_id'     => $plug_id,
                'request_parameter' => serialize( $value['request_parameter'] ),
                'fields'        => serialize( $value['fields'] )
            ));
        }
        return true;
    }
    
}

